package com.hive.controller;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ClassName HiveController Description TODO
 *
 * @author qq
 * @date 2023/8/2 14:21
 * @since JDK 1.8
 */
@RestController
@Slf4j
public class HiveController {

  @Autowired
  private DataSource hiveDruidDataSource;
  @Autowired
  @Qualifier("hiveDruidDataSource")
  private DataSource druidDataSource;

  @RequestMapping("/sql/execute")
  public List<String> execute(String sql) {
    List<String> list = new ArrayList<>();
    Statement statement = null;
    try {
      statement = hiveDruidDataSource.getConnection().createStatement();
      log.info("Running: " + sql);
      ResultSet res = statement.executeQuery(sql);
      while (res.next()) {
        list.add(res.getString(1));
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return list;
  }

}
